package com.symphony.crypto;

import java.util.Random;

public class NumbersUtil {
	
	public static boolean isPrime(int number) {
		int border = (int) Math.sqrt(number);
		for(int i = 2; i <= border; i++) {
			if(number % i == 0)
				return false;
		}
		return true;
	}
	
	public static int generateRandomPrime() {
		int result = 0;
		Random rand = new Random();
		while(!(result > 0) ) {
			int variant = rand.nextInt();
			if(isPrime(variant)) {
				result = variant;
			}
		}
		return result;
	}
	
	public static int generateRandomRelativelyPrime(int n) {
		int result = 0;
		Random rand = new Random();
		while(!(result > 0) ) {
			int possible = rand.nextInt(n);
			if(isPrime(possible) && (n % possible != 0)) {
				result = possible;
			}
		}
		return result;
	}
	
	public static int fi(int p, int q) {
		return (p - 1) * (q - 1);
	}
	
	public static int simpleEucklidAlgorithm(int a, int b) {
		return b != 0 ? simpleEucklidAlgorithm(b, a % b) : a;
	}
	
	public static int gcd(int a, int b){
		if(b==0)
			return a;
		else 
			return gcd(b, a % b);
	}
}
